package cn.bocweb.spinning.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ItemBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.ItemAdapter;

/**
 * Created by chenchangxing on 16/8/19.
 */
public class ItemDialog extends AlertDialog {

    List<ItemBean> list;
    ItemAdapter itemAdapter;
    OnConfirmListener onConfirmListener;
    int selected;
    int confirmed;

    @Bind(R.id.gridview_item)
    GridView gridviewItem;
    @Bind(R.id.button_submit)
    Button buttonSubmit;

    public ItemDialog(Context context, List<ItemBean> list) {
        super(context);
        this.list = list;

    }

    protected ItemDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_item);
        ButterKnife.bind(this);

        itemAdapter = new ItemAdapter(this.list);
        gridviewItem.setAdapter(itemAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();

        itemAdapter.setSelection(confirmed);
        itemAdapter.notifyDataSetChanged();

        itemAdapter.setOnItemClickListener(new ItemAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                selected = position;
                itemAdapter.setSelection(position);
                itemAdapter.notifyDataSetChanged();
            }
        });

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmed = selected;
                onConfirmListener.onClick(confirmed, list.get(confirmed).getId());
                dismiss();
            }
        });
    }

    public void loadData(List<ItemBean> list) {
        DeviceUtil.Log("" + list.size());
        this.list.addAll(list);
        DeviceUtil.Log("itemadapter" + list.size());
    }

    public void setConfirmed(int confirmed) {
        this.confirmed = confirmed;
    }

    public interface OnConfirmListener {
        void onClick(int confirm, String id);
    }

    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }
}
